package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Supplier;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.V;
import com.by.query.SupplierQuery;
import com.by.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(tags = "供应商")
@RequestMapping("/api")
public class SupplierController extends BaseController  {

    @Autowired
    SupplierService supplierService;

    @ApiOperation("查询供应商")
    @GetMapping("/supplier")
    public ResultBean get(@RequestParam String id) {
        Supplier supplier = supplierService.get(id);
        return success("供应商信息", supplier);
    }
    @PostMapping("/supplier")
    @ApiOperation("保存")
    public ResultBean save(@Validated @RequestBody Supplier supplier, BindingResult result){
        V.validate(result);
        int rs = supplierService.save(supplier);
        return saveResult(rs);
    }

    @PostMapping("/supplier/list")
    @ApiOperation("查询供应商列表")
    public ResultBean<PageBean<List<Supplier>>> query(@RequestBody SupplierQuery supplier) {
        PageBean bean = supplierService.queryByMap(BeanUtil.beanToMap(supplier));
        return success("供应商列表", bean);
    }
    @ApiImplicitParam(name = "id", value = "供应商ID",dataTypeClass = Integer.class,paramType = "query",required = true)
    @ApiOperation("删除员工")
    @DeleteMapping("/supplier")
    public ResultBean delete(@RequestParam(required = true) int id){
        int rs = supplierService.delete(id);
        return deleteResult(rs);
    }

    @ApiOperation(("修改客户"))
    @PutMapping("/supplier")
     public ResultBean update(@RequestBody Supplier supplier){
        int rs = supplierService.update(supplier);
        return updateResult(rs);
    }
}

